function SDX_Tables(_RootObject)
{
 this.RootObject = _RootObject ;
 this.Tables=new Array();
 this.InsertTable=InsertTable;
 function InsertTable(_Rows, _GroupBy)
 {
  this.Tables[this.Tables.length]=new SDX_Table(this.RootObject, this.Tables.length, _Rows, _GroupBy);
  return this.Tables[this.Tables.length-1];
 }
 function SDX_Table(_RootObject , _ID, _Rows, _GroupBy)
 {
  this.RootObject = _RootObject ;
  this.ID = _ID;
  this.HTMLID=this.RootObject.Name + "_SDX_Table_" + _ID;
  this.RowsHTMLID=this.RootObject.Name + "_SDX_RowsTable_" + _ID;
  this.HTMLOutPut = HTMLOutPut;
  this.RePaint = RePaint;
  this.GroupBy = _GroupBy;
  this.Navigator = new SDX_Navigator(this.RootObject,this) ;
  this.Columns = new SDX_Columns(this.RootObject,this) ;
  this.Navigator.Table = this ;
  this.OriginalRows = _Rows;
  this.Navigator.RowLimit = this.RootObject.RowLimit;
  this.Rows = new Array();
  this.AddNewRow = AddNewRow ;
  this.GetEvalObjectString = GetEvalObjectString ;
  this.Initialize = Initialize
  for(var nX in _Rows)
  {
   this.Navigator.FilteredRows[this.Navigator.FilteredRows.length] = _Rows[nX] ;
   this.Rows[this.Rows.length] = _Rows[nX] ;
   _Rows[nX].DataTable = this ;
  }
  function GetEvalObjectString()
  {
   return this.RootObject.Name + ".Tables.Tables[" + this.ID + "]" ;
  }
  function AddNewRow()
  {
   var fields = this.RootObject.Columns.Columns ;
   var data = new Array() ;
   for(var nX in fields)
   {
    if(this.RootObject.Columns.Columns[nX].AutoIncrement == true)
    {
     this.RootObject.Columns.Columns[nX].LastAutoIncrementID -- ;
     data[nX] = this.RootObject.Columns.Columns[nX].LastAutoIncrementID ;
    }
    else
    {
     data[nX] = this.RootObject.Columns.Columns[nX].DefaultValue ;
    }
   }
   var groupBy = this.GroupBy;
   while(groupBy != null)
   {
    data[groupBy.GroupColumn.ID] = groupBy.CellValue ;
    groupBy = groupBy.ParentRow ;
   }
   var currentDataIndex = this.RootObject.Datas.InsertData(data) ;
//   var row = this.RootObject.Rows.InsertRow(this.RootObject.Datas.CurrentDatas[currentDataIndex]);
   var row = this.RootObject.Rows.InsertRow(currentDataIndex, this.RootObject);
   this.OriginalRows[this.OriginalRows.length] = row
   this.Navigator.FilteredRows[this.Navigator.FilteredRows.length] = row ;
   this.Rows[this.Rows.length] = row ;
   row.DataTable = this ;
   var tbl = document.getElementById(this.RowsHTMLID);
   var lastRow = tbl.rows.length;
   var htmlrow = tbl.insertRow(lastRow);
   var tableString = tbl.parentNode.innerHTML ;
   var tableEndIndex = tableString.lastIndexOf("</TABLE>") ;
   var beforeRowString = tableString.substr(0,tableEndIndex) ;
   var newTableString = beforeRowString + row.GetRowView().HTMLOutPut() + "</TABLE>" ;
   tbl.parentNode.innerHTML = newTableString ;
   row.GetRowView().ChangeSelectionStatus(2) ;
  }
  function ChangePageIndex(nIndex)
  {
   if(isNaN(nIndex))
   {
    alert("Wrong entry");
    return;
   }
   if(this.PageIndex==nIndex)
    return;
   var nPageCount=Math.ceil(this.Navigator.FilteredRows.length/this.RowLimit);
   if(nIndex>nPageCount)
   {
    alert("Your selected page index is greater than the page count");
    return;
   }
   if(nIndex<1)
   {
    alert("Your selected page index must be greater than 0");
    return;
   }
   this.PageIndex=nIndex;
   this.RePaint();
  }
  function ChangeRowLimit(nRowCount)
  {
   this.RowLimit=nRowCount;
   this.RePaint();
  }
  function Sort(nColumnIndex,bNormal,bNoRePaint)
  {
   this.SortColumnIndex=nColumnIndex;
   this.SortRotation=bNormal;
   SDXActiveGrid.LastSortColumnIndex=nColumnIndex;
   if(bNormal)
   {
    this.Navigator.FilteredRows.sort(this.verseSort)
   }
   else
   {
    this.Navigator.FilteredRows.sort(this.reverseSort)
   }
   this.Navigator.FilteredRows=new Array()
   for(var nX in this.Rows)
    this.Navigator.FilteredRows[nX]=this.Rows[nX]
   if(!bNoRePaint)this.RePaint();
  }
  function reverseSort(a,b)
  {
   var nColumnIndex=SDXActiveGrid.LastSortColumnIndex;
   
   if(SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(a.Data[nColumnIndex]) > SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(b.Data[nColumnIndex]))
    return -1
   if(SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(a.Data[nColumnIndex]) < SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(b.Data[nColumnIndex]))
    return 1
   return 0
  }
  function verseSort(a,b)
  {
   var nColumnIndex=SDXActiveGrid.LastSortColumnIndex;
   if(SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(a.Data[nColumnIndex]) > SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(b.Data[nColumnIndex]))
    return 1
   if(SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(a.Data[nColumnIndex]) < SDXActiveGrid.Columns.Columns[nColumnIndex].GetText(b.Data[nColumnIndex]))
    return -1
   return 0
  }
  function RePaint()
  {
   if(this.RootObject.Rows.EditedCell!=null)
    this.RootObject.Rows.EditedCell.NormalMode()
   this.RootObject.Rows.ClearSelectedRows()
   document.getElementById(this.HTMLID).parentNode.innerHTML=this.HTMLOutPut()
  }
  function HTMLOutPut()
  {
   var sRows = this.RootObject.Columns.GetHTMLOutPut(this) ;
   var nX = this.Navigator.RowLimit*(this.Navigator.PageIndex-1);

   do
   {   
    if(!this.Navigator.FilteredRows[nX])
     break;

    if(this.RootObject.Rows.Rows[this.RootObject.GetRowMainArrayIndex(this.Navigator.FilteredRows[nX].RowIndex)].IsDeleted == true)
    {
     nX++ ;
     continue;
    }

    this.Navigator.FilteredRows[nX].Table=this;
    var rowView = this.Navigator.FilteredRows[nX].GetRowView() ;
//    this.Navigator.FilteredRows[nX].CellClass=((this.PageIndex+nX)%2==0?"SmlTblLine1":"SmlTblLine2")
    sRows+=rowView.HTMLOutPut();
    nX++;
   }
   while(nX<this.Navigator.RowLimit*(this.Navigator.PageIndex));

   var sHTMLOutPut="<table id=\"" + this.HTMLID + "\" width=\"100%\" border=\"0\" cellspacing=0 cellpadding=0>" ;
       if(this.RootObject.AllowSearch==true )
       {
           sHTMLOutPut += " <tr>" +
                           "  <td id=\"" + this.Navigator.GetSearchPanelHTMLID() + "\">" +
                           this.Navigator.GetSearchPanelHTMLOutPut() +
                           "  </td>" +
                           " </tr>"
       }
       sHTMLOutPut += " <tr>" +
                   "  <td id=\"sdxTable\">" +
                   "   <table width='100%' id=\"" + this.RowsHTMLID + "\" " + this.RootObject.Styles.GetStyleHTML(this.RootObject.Styles.Style) + " CellSpacing=" + this.RootObject.Styles.Style.CellSpacing + " CellPadding=" + this.RootObject.Styles.Style.CellPadding + "  >" +
                   sRows +
                   "   </table>" +
                   "  </td>" +
                   " </tr>" ;
       if(this.RootObject.AllowEdit==true || this.RootObject.AllowAdd==true || this.RootObject.AllowDelete==true || this.RootObject.AllowSearch==true)
       {
           sHTMLOutPut += " <tr>" +
                          "  <td id=\"" + this.Navigator.GetPagingPanelHTMLID() + "\">" +
                          this.Navigator.GetPagingPanelHTMLOutPut() +
                          "  </td>" +
                          " </tr>" 
       }
       sHTMLOutPut += "</table>"
   return sHTMLOutPut;
  }
  function Initialize()
  {
  }
 }
}
